/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   CreateScriptableObject.cs
 * CreateData   :   2023/5/25 13:21:18
 * UnityVersion :   2021.3.20f1c1
 * Description  :   创建一个ScriptableObject资源并返回
************************************************/

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;

namespace Main
{
    public class CreateScriptableObject
    {
        public static Object GetScriptableObject<T, U>(string path) where T : ScriptableObjectConfigBase<U>
        {
            T instance = AssetDatabase.LoadAssetAtPath<T>(path);
            if (instance == null)
            {
                instance = (T)ScriptableObject.CreateInstance(typeof(T));
                instance.ScriptableObject = instance;
                string guid = AssetDatabase.FindAssets("t:Script " + typeof(T).Name, new string[] { PathConfig.MainScriptPath })[0];
                instance.ScriptFile = AssetDatabase.LoadAssetAtPath<MonoScript>(AssetDatabase.GUIDToAssetPath(guid));
                AssetDatabase.CreateAsset(instance, path);
                AssetDatabase.SaveAssets();
            }
            return instance;
        }
    }
}
#endif
